package com.example.weather_news.fragment.frag_items;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.example.weather_news.R;
import com.example.weather_news.activity.WeatherFutureActivity;
import com.example.weather_news.adapter.Adapter_Hours;
import com.example.weather_news.fragment.Fragment_Weather;
import com.example.weather_news.http.HttpRequest;
import com.example.weather_news.view.SpaceItemDecoration;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by 逝 on 2017/06/14.
 */

public class Frag_Weather extends Fragment {
    private static final String CITY = "CITY";

    private RelativeLayout relative;
    private TextView cityName;
    private TextView cityType;
    private TextView cityTemp;
    private RecyclerView recycler_24hours;
    private TextView tv_today_weather;
    private TextView tv_today_temp;
    private ImageView iv_today;
    private TextView tv_tomo_weather;
    private TextView tv_tomo_temp;
    private ImageView iv_tomo;
    private TextView tv_future7;
    private TextView tv_air_conditioning;
    private TextView tv_cold_index;
    private TextView tv_car_index;
    private TextView tv_clothes_index;
    private TextView tv_light_index;
    private TextView tv_sports_index;
    private ScrollView scroll_weather;
    private SwipeRefreshLayout refresh_weather;


    private JSONArray ja;
    private Adapter_Hours adapter;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable
            Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.frag_weather, null);
        initView(view);
        return view;
    }

    public static Frag_Weather inseryWeather(String city) {
        Bundle bundle = new Bundle();
        Frag_Weather weather = new Frag_Weather();
        bundle.putString(CITY, city);
        weather.setArguments(bundle);
        return weather;
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        final String city = getArguments().getString(CITY);
        Log.e("city", city);
        cityName.setText(city);

        if (!refresh_weather.isRefreshing()) {
            refresh_weather.setRefreshing(true);
            relative.setVisibility(View.GONE);
            getCityWeather(city);
        }

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        recycler_24hours.setLayoutManager(layoutManager);
        adapter = new Adapter_Hours(getActivity());

        //设置水平RecyclerView的item之间的间距
        recycler_24hours.addItemDecoration(new SpaceItemDecoration(0, 20));

        recycler_24hours.setAdapter(adapter);

        //刷新
        refresh_weather.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getCityWeather(city);
            }
        });
        //查看未来7天天气
        tv_future7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getActivity(), WeatherFutureActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("cityName", city);
                intent.putExtras(bundle);
                startActivity(intent);
                getActivity().
                        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
            }
        });
    }

    private void getCityWeather(String city) {
        ja = null;
        HttpRequest.requestWeather(city, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                    if (jsonObject.getInt("status") != 0) {
                        Toast.makeText(getActivity(), jsonObject.getString("msg"), Toast
                                .LENGTH_SHORT).show();
                        return;
                    }
                    String result = jsonObject.getString("result");

                    JSONObject jo = new JSONObject(result);
                    String weather = jo.getString("weather");
                    String temp = jo.getString("temp");
                    String temphigh = jo.getString("temphigh");

                    int img = jo.getInt("img");
                    setBackGround(img);
                    setWeatherImg(iv_today, img);

                    cityType.setText(weather);
                    cityTemp.setText(temp + "°");
                    tv_today_weather.setText(weather);
                    tv_today_temp.setText(temphigh + "°");

                    String daily = jo.getString("daily");

                    //获取明天天气
                    JSONArray jsonArray = new JSONArray(daily);
                    JSONObject j = jsonArray.getJSONObject(1);
                    String day = j.getString("day");
                    JSONObject o = new JSONObject(day);
                    tv_tomo_weather.setText(o.getString("weather"));
                    tv_tomo_temp.setText(o.getString("temphigh") + "°");
                    setWeatherImg(iv_tomo, o.getInt("img"));

                    //24小时预报
                    String hourly = jo.getString("hourly");
                    ja = new JSONArray(hourly);
                    adapter.setData(ja);
                    Log.e("ja", ja.length() + "");

                    //生活助手
                    String index = jo.getString("index");
                    JSONArray array = new JSONArray(index);

                    JSONObject object;
                    String ivalue = "";
                    String detail = "";
                    for (int i = 0; i < array.length(); i++) {
                        object = array.getJSONObject(i);
                        ivalue = object.getString("ivalue");
                        detail = object.getString("detail");
                        setLivingIndex(i, ivalue);
                    }
                    if (refresh_weather.isRefreshing()) {
                        refresh_weather.setRefreshing(false);
                    }

                    relative.setVisibility(View.VISIBLE);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, null);
    }

    //设置天气图标
    private void setWeatherImg(ImageView iv, int img) {
        switch (img) {
            case 0:
                iv.setBackgroundResource(R.mipmap.qing);
                break;
            case 1:
                iv.setBackgroundResource(R.mipmap.duoyun_bai);
                break;
            case 2:
                iv.setBackgroundResource(R.mipmap.yin);
                break;
            case 3:
                iv.setBackgroundResource(R.mipmap.zhenyu);
                break;
            case 4:
                iv.setBackgroundResource(R.mipmap.leizhenyu);
                break;
            case 5:
                iv.setBackgroundResource(R.mipmap.ic_launcher_round);
                break;
            case 6:
                iv.setBackgroundResource(R.mipmap.ic_launcher_round);
                break;
            case 7:
                iv.setBackgroundResource(R.mipmap.xiaoyu);
                break;
            case 8:
                iv.setBackgroundResource(R.mipmap.zhongyu);
                break;
            case 9:
                iv.setBackgroundResource(R.mipmap.dayu);
                break;
            case 10:
                iv.setBackgroundResource(R.mipmap.baoyu);
                break;
            case 22:
                iv.setBackgroundResource(R.mipmap.zhongyu);
                break;
            default:
                iv.setBackgroundResource(R.mipmap.ic_launcher_round);
                break;
        }
    }

    //设置天气图标
    private void setBackGround(int img) {
        switch (img) {
            case 0:
                refresh_weather.setBackgroundResource(R.mipmap.bg_qing);
                break;
            case 1:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_day);
                break;
            case 2:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
            case 3:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
            case 4:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
            case 5:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
            case 6:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
            case 7:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
            case 8:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
            case 9:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
            case 10:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
            case 22:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                Fragment_Weather.iv_weather_back.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
            default:
                refresh_weather.setBackgroundResource(R.mipmap.bg_fog_and_haze);
                break;
        }
    }

    //生活提示
    private void setLivingIndex(int i, String ivalue) {
        switch (i) {
            case 0:
                tv_air_conditioning.setText(ivalue);
                break;
            case 1:
                tv_sports_index.setText(ivalue);
                break;
            case 2:
                tv_light_index.setText(ivalue);
                break;
            case 3:
                tv_cold_index.setText(ivalue);
                break;
            case 4:
                tv_car_index.setText(ivalue);
                break;
            case 5:
                break;
            case 6:
                tv_clothes_index.setText(ivalue);
                break;
        }
    }


    private void initView(View view) {
        relative = (RelativeLayout) view.findViewById(R.id.relative);
        cityName = (TextView) view.findViewById(R.id.cityName);
        cityType = (TextView) view.findViewById(R.id.cityType);
        cityTemp = (TextView) view.findViewById(R.id.cityTemp);
        recycler_24hours = (RecyclerView) view.findViewById(R.id.recycler_24hours);
        tv_today_weather = (TextView) view.findViewById(R.id.tv_today_weather);
        tv_today_temp = (TextView) view.findViewById(R.id.tv_today_temp);
        iv_today = (ImageView) view.findViewById(R.id.iv_today);
        tv_tomo_weather = (TextView) view.findViewById(R.id.tv_tomo_weather);
        tv_tomo_temp = (TextView) view.findViewById(R.id.tv_tomo_temp);
        iv_tomo = (ImageView) view.findViewById(R.id.iv_tomo);
        tv_future7 = (TextView) view.findViewById(R.id.tv_future7);
        tv_air_conditioning = (TextView) view.findViewById(R.id.tv_air_conditioning);
        tv_cold_index = (TextView) view.findViewById(R.id.tv_cold_index);
        tv_car_index = (TextView) view.findViewById(R.id.tv_car_index);
        tv_clothes_index = (TextView) view.findViewById(R.id.tv_clothes_index);
        tv_light_index = (TextView) view.findViewById(R.id.tv_light_index);
        tv_sports_index = (TextView) view.findViewById(R.id.tv_sports_index);
        scroll_weather = (ScrollView) view.findViewById(R.id.scroll_weather);
        refresh_weather = (SwipeRefreshLayout) view.findViewById(R.id.refresh_weather);
    }
}
